Object.assign(global,{
    R:{
        REGISTER_INDEX:100,
        next:function(){//用于注册的计数器
            return R.REGISTER_INDEX++;
        },
        set:function(name,data){
            if(R[name] == undefined){
                R[name] = data;
                return true;
            }else{
                return false;
            }
        },
        SHARD_0:0,
        SHARD_1:1,
        SHARD_2:2,
        SHARD_3:3,

        STRUCTURE_SPAWN:4,
    }
});

global.Registry = {

    //分类快速访问表
    Rooms:[],
    Creeps:[],
    Structures:[],
    
    rooms:function(arr){//定义房间
        for(let data of arr){
            let use = data.use;
            let define = data.define;

            //数据检查
            for(let i in use){
                if(use[i] == undefined) throw Error("[CODE:10001]字段 "+ i +" 没有找到对应的注册数据");
            }

            //封装数据
            let pack = {
                shard:use.shard,
                position:define.position,
                timeDilation:define.timeDilation || 0,
            }

            //注册
            if(!R.set(data.name,pack)) throw Error("[CODE:10002]房间名称已被使用");

            //写入快速访问
            Registry.Rooms.push(data.name);


            //创建TimeDilation
            
            TimeDilation.creat(define.position,define.timeDilation,0);
        }
    },

    structures:function(arr){
        for(let data of arr){
            let use = data.use;
            let define = data.define;

            //数据检查
            for(let i in use){
                if(use[i] == undefined) throw Error("[CODE:10001]字段 "+ i +" 没有找到对应的注册数据");
            }

            //封装数据
            let pack = {
                type:use.type,
            }

            for(let key in define.structureData){
                pack[key] = define.structureData[key];
            }
            
            //注册
            if(!R.set(data.name,pack)) throw Error("[CODE:10002]名称已被使用");

            //写入快速访问
            Registry.Structures.push(data.name);
        }
    },

    creeps:function(arr){//定义Creep
        for(let data of arr){

            let use = data.use;
            let define = data.define;

            //数据检查
            for(let i in use){
                if(use[i] == undefined) throw Error("[CODE:10001]字段 "+ i +" 没有找到对应的注册数据");
            }

            //封装数据，并自动补全默认数据
            let pack = {
                spawn:use.spawn,
                body:define.body || [MOVE],
                event:define.event || {},
            }
            //注册
            if(!R.set(data.name,pack)) throw Error("[CODE:10002]房间名称已被使用");

            //写入快速访问
            Registry.Creeps.push(data.name);
        }
    },
    states:function(arr){//定义Creep的行为
        // for(let data of arr){
        //     Registry.state[data.name] = data.conduct;
        // }
    }
}


